﻿using System;
using System.Data;
using SqlSugar;

namespace MS.Core.BaseProvider
{
    /// <summary>
    /// 
    /// </summary>
    /// <typeparam name="TEntity"></typeparam>
    public class BaseRepository<TEntity> : IBaseRepository<TEntity> where TEntity : class, new()
    {
        public SqlSugarScope DB { get; set; }
        /// <summary>
        /// 构造函数
        /// </summary>
        /// <param name="sqlSugarScope"></param>
        public BaseRepository(SqlSugarScope sqlSugarScope)
        {
            this.DB = sqlSugarScope;
        }
        /// <summary>
        /// 插入一条数据
        /// </summary>
        /// <param name="entity"></param>
        /// <returns></returns>
        public int Insert(TEntity entity)
        {
            return DB.Insertable(entity).ExecuteCommand();
        }
        /// <summary>
        /// 插入数据返回雪花id
        /// </summary>
        /// <param name="entity"></param>
        /// <returns></returns>
        public long ExecuteReturnSnowflakeId(TEntity entity)
        {
            return DB.Insertable(entity).ExecuteReturnSnowflakeId();
        }

        /// <summary>
        /// 插入多条数据返回雪花id
        /// </summary>
        /// <param name="entity"></param>
        /// <returns></returns>
        public List<long> ExecuteRecordsSnowflakeId(List<TEntity> entitys)
        {
            return DB.Insertable(entitys).ExecuteReturnSnowflakeIdList();
        }

        /// <summary>
        /// 删除一条数据
        /// </summary>
        /// <param name="entity"></param>
        /// <returns></returns>
        public int Delete(TEntity entity)
        {
            return DB.Deleteable(entity).ExecuteCommand();
        }
        /// <summary>
        /// 更新一条数据
        /// </summary>
        /// <param name="entity"></param>
        /// <returns></returns>
        public int Update(TEntity entity)
        {
            return DB.Updateable(entity).ExecuteCommand();
        }
     
        /// <summary>
        /// 通过主键获取一条记录
        /// </summary>
        /// <param name="id"></param>
        /// <returns></returns>
        public TEntity GetById(object id)
        {
            return DB.Queryable<TEntity>().InSingle(id);
        }
 
        
        /// <summary>
        /// 传入sql获取DataTable
        /// </summary>
        /// <param name="sql"></param>
        /// <returns></returns>
        public DataTable GetDataTable(string sql)
        {
            return DB.Ado.GetDataTable(sql);
        }
        /// <summary>
        /// 执行一条sql
        /// </summary>
        /// <param name="sql"></param>
        /// <returns></returns>
        public int ExecuteCommand(string sql)
        {
            return DB.Ado.ExecuteCommand(sql);
        }
        /// <summary>
        /// 传入查询条件获取列表数据
        /// </summary>
        /// <param name="exp"></param>
        /// <returns></returns>
        public List<TEntity> GetListByWhere(Expressionable<TEntity> exp) 
        {
            if (exp != null)
            {
                return DB.Queryable<TEntity>().Where(exp.ToExpression())?.ToList();
            }
            else 
            {
                return DB.Queryable<TEntity>().ToList();
            }
           
        }
    }
}
